#include "TimeMeasurement.h"

#ifdef WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#endif

#include <stdlib.h>

void *StartTimeMeasurement()
{
#ifdef WIN32
	LARGE_INTEGER *PerformanceCount = (LARGE_INTEGER *)malloc(sizeof(LARGE_INTEGER));
	QueryPerformanceCounter(PerformanceCount);
	return PerformanceCount;
#else
	struct timeval *time_start = (struct timeval *)malloc(sizeof(struct timeval));
	gettimeofday(time_start, NULL);
	return time_start;
#endif
}

double EndTimeMeasurement(void *Start, int NumTestRuns)
{
#ifdef WIN32
	LARGE_INTEGER *TimeStart = (LARGE_INTEGER *)Start;
	LARGE_INTEGER Frequency;
	LARGE_INTEGER End;
	double Time;
	QueryPerformanceCounter(&End);
	QueryPerformanceFrequency(&Frequency);
	Time = (double)(End.QuadPart - TimeStart->QuadPart) / (double)Frequency.QuadPart / NumTestRuns * 1E6;
	free(Start);
	return Time;
#else
	struct timeval *TimeStart = (struct timeval *)Start;
	struct timeval End;
	double Time;
	gettimeofday(&End, NULL);
	Time = (double)((End.tv_sec - TimeStart->tv_sec) * 1E6 + End.tv_usec - TimeStart->tv_usec) / NumTestRuns;
	free(Start);
	return Time;
#endif
}
